package com.shitou.shitoucart.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.amqp.core.TopicExchange;

import org.springframework.amqp.core.Exchange;

import java.util.HashMap;
import java.util.Map;


/**
 * class detail:
 *
 * @author 石头人
 */
@SuppressWarnings("all")
@Configuration
public class RabbitMQConfig {

    public final static String ADDCART_EXCHANGE = "SHITOU-CART-EXCHANGE";

    public final static String ADDCART_QUEUE = "CART-ADDCART-QUEUE";
    public final static String TEST_QUEUE = "CART-TEST-QUEUE";
    public final static String TTL_QUEUE = "CART-TTL-QUEUE";
    public final static String DELETE_CART = "DELETE-CART";



    public final static String ADDCART_ROUTING_KEY = "cart.create";


    /**
     * 购物车交换机
     *
     * @return
     */
    @Bean
    public Exchange exchanger() {
        /**
         * 队列名称，是否持久化，是否自动删除，参数用于制作一些操作
         */
        return new TopicExchange(ADDCART_EXCHANGE, true, false, null);
    }


    @Bean
    public Queue ttl1Queue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", ADDCART_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", ADDCART_ROUTING_KEY);
        arguments.put("x-message-ttl", 10000); // 单位:毫秒 办分钟仅仅用于测试，实际根据需求，通常30分钟或者15分钟
        return new Queue(TTL_QUEUE, true, false, false, arguments);
    }


    @Bean
    public Binding addCartBinding() {
        /**
         * 绑定的队列
         * 绑定的类型
         * 将队列绑定到某某交换机
         * 路由关键字
         * 参数
         */
        return new Binding(ADDCART_QUEUE, Binding.DestinationType.QUEUE, ADDCART_EXCHANGE, ADDCART_ROUTING_KEY, null);
    }

    /**
     * 添加购物车,判断库存,,,
     * routingKey: cart.create
     */
    @Bean
    public Queue addCartQueue() {
        return new Queue(ADDCART_QUEUE, true, false, false, null);
    }

    @Bean
    public Binding ttlBinding() {
        return new Binding(TTL_QUEUE, Binding.DestinationType.QUEUE, ADDCART_EXCHANGE, ADDCART_ROUTING_KEY, null);
    }


    @Bean
    public Queue testQueue() {
        return new Queue(TEST_QUEUE, true, false, false, null);
    }



    @Bean
    public Queue deleteCart(){
        return new Queue(DELETE_CART, true, false, false, null);
    }



}
